package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.service.ReportService;
import com.itheima.health.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：seanyang
 * @date ：Created in 2019/6/29
 * @description ：
 * @version: 1.0
 */
@Service
@Slf4j
public class ReportServiceImpl implements ReportService {

	@Autowired
	private MemberDao memberDao;

	@Autowired
	private OrderDao orderDao;


	@Override
	public Map<String, Object> getBussinessReport() {
		// 1. 获得今天日期
		// 2. 获得本周周一日期
		// 3. 获得本月第一天日期
		String today = "";
		String weekMonday = "";
		String firstDayInMonth = "";
		try{
			today = DateUtils.parseDate2String(new Date());
			weekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
			firstDayInMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
		}catch(Exception e){
		    e.printStackTrace();
		    throw new RuntimeException("初始化日期失败");
		}
		// 4. 获取与会员有关数据 (今日、本周、本月)
		Integer todayNewMember = memberDao.findMemberCountByDate(today);
		Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(weekMonday);
		Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDayInMonth);
		Integer totalNumber = memberDao.findMemberTotalCount();
		// 5. 获取与预约有关数据
		Integer todayOrderNumber = orderDao.findOrderCountByDate(today);
		Integer thisWeekOrderNumber = orderDao.findOrderCountByAfterDate(weekMonday);
		Integer thisMonthOrderNumber = orderDao.findOrderCountByAfterDate(firstDayInMonth);
		// 6. 获得到诊相关数据
		Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);
		Integer thisWeekVisitsNumber = orderDao.findVisitsCountByAfterDate(weekMonday);
		Integer thisMonthVisitsNumber = orderDao.findVisitsCountByAfterDate(firstDayInMonth);
		// 7. 获得套餐热门数据
		List<Map> hotSetmeal = orderDao.findHotSetmeal();
		// 8. 封装数据到map集合
		Map<String,Object> result = new HashMap<>();
		result.put("reportDate",today);
		// 会员相关
		result.put("todayNewMember",todayNewMember);
		result.put("thisWeekNewMember",thisWeekNewMember);
		result.put("thisMonthNewMember",thisMonthNewMember);
		result.put("totalMember",totalNumber);
		// 预约相关
		result.put("todayOrderNumber",todayOrderNumber);
		result.put("thisWeekOrderNumber",thisWeekOrderNumber);
		result.put("thisMonthOrderNumber",thisMonthOrderNumber);
		// 到诊相关
		result.put("todayVisitsNumber",todayVisitsNumber);
		result.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
		result.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
		// 套餐数据
		result.put("hotSetmeal",hotSetmeal);
		return result;
	}
}
